package com.example.treehole.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.treehole.R;
import com.example.treehole.utils.StatusBarUtil;

import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


public class PhotoActivity extends BaseActivity {

    @BindView(R.id.view_page)
    ViewPager viewPage;
    @BindView(R.id.tv_page)
    TextView tvPage;

    List<String> imgUrls = new ArrayList<>();
    int position = 0;

    @Override
    protected int initLayout() {
        return R.layout.activity_photo;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initData() {
        StatusBarUtil.setColorNoTranslucent(this, Color.BLACK);

        receiveData();

        viewPage.setAdapter(new MyPageAdapter(this, imgUrls));
        viewPage.setCurrentItem(position);
        tvPage.setText(position+1+"/"+imgUrls.size());
        viewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                tvPage.setText(position+1+"/"+imgUrls.size());
            }
            @Override
            public void onPageSelected(int position) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void receiveData() {
        String type=getIntent().getStringExtra("type");
        position=getIntent().getIntExtra("position",0);
        if (type.equals(getString(R.string.URL)))
            imgUrls=getIntent().getStringArrayListExtra("data");
        else if (type.equals(getString(R.string.PATH))){
            List<LocalMedia> list=getIntent().getExtras().getParcelableArrayList("data");
            for (int i = 0; i < list.size()-1; i++) {
                imgUrls.add("file://"+list.get(i).getPath());
            }
        }
    }


    class MyPageAdapter extends PagerAdapter {
        Context context;
        List<String> imgUrls;


        public MyPageAdapter(Context context, List<String> imgUrls) {
            this.context = context;
            this.imgUrls = imgUrls;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
            Picasso.get()
                    .load(imgUrls.get(position))
                    .into(photoView, new Callback() {
                        @Override
                        public void onSuccess() {
                            attacher.update();
                        }

                        @Override
                        public void onError(Exception e) {
                        }
                    });
            attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
                @Override
                public void onOutsidePhotoTap() {
                }
            });
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public int getCount() {
            return imgUrls.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

}